package com.xxzb.db;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DBManager {
    private static DataSource dataSource;
    static {
        try {
            //创建一个读取properties文件的类
            Properties properties=new Properties();
            //通过类加载器的方法读取这个文件
            InputStream in = DBManager.class.getClassLoader().getResourceAsStream("druid.properties");
            //加载这个文件
            properties.load(in);
            //产生一个数据源
            dataSource= DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //通过阿里的Druid连接数据库的方式
    public static Connection getConnection() throws Exception{
        Connection conn=dataSource.getConnection();
        return conn;
    }
}
